//管理用户数据相关
import {defineStore} from "pinia";
import {ref} from "vue";
import {useMenuInfoAPI, useFullMenuInfoPI, useMenuByPidLevelAPI, useBaseMenusAPI
    ,useChoseMenusAPI } from "@/apis/menuInfo.ts";

export const useMenuStore=defineStore('menu',()=>{

        //1.定义菜单的数据的state
        const menuInfo=ref([]);
        // 完整的菜单树数据
        const fullMenuTree=ref([]);
        // 根据pid和level获取到的菜单数据
        const menuTreeByPidLevel=ref([]);
        // 根据登录用户的所在地，角色获取不同的第一页菜单基本数据
        const firstMenus=ref([]);
        // 根据数据库的pid，level获取到的菜单
        const choseMenu=ref([]);


        //2.定义获取接口数据的action函数
        const getMenu=async ()=>{
            const res=await useMenuInfoAPI()
            menuInfo.value=res.data;
        }
        // 获取完整菜单树数据
        const getFullMenuTree=async ()=>{
            const res=await useFullMenuInfoPI();
            fullMenuTree.value=res.data;
        }
        //根据pid及level获取菜单树
        const getMenuTreeByPidLevel=async (pid:number,level:number)=>{
            const res=await useMenuByPidLevelAPI(pid,level);
            menuTreeByPidLevel.value=res.data;
        }

        const getFirstMenus=async ()=>{
            const res=await useBaseMenusAPI();
            firstMenus.value=res.data;
        }

        const getMenuByPidLevel=async (pid:number,level:number)=>{
            const res=await useChoseMenusAPI(pid,level);
            choseMenu.value=res.data;
    }



        //3.以对象的格式把state和action return
        return {
            menuInfo,getMenu,fullMenuTree,getFullMenuTree,
            getMenuTreeByPidLevel,menuTreeByPidLevel,
            firstMenus,getFirstMenus,
            getMenuByPidLevel,choseMenu
        }
    }

)